package com.ybkj.daijia.tags;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.Config;
import com.ybkj.daijia.server.model.AppVersion;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.service.AppVersionService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverApplyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberApplyService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.ReceiptService;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * Created with IntelliJ IDEA. User: Shine Date: 13-4-13 Time: 上午10:13 To change this template use
 * File | Settings | File Templates.
 */
public class Functions extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = -3166464343900935123L;

    // 未分派订单
    public static long allNewOrderNums() {
        OrderService orderService = Config.getWac().getBean(OrderService.class);
        return orderService.countByStatus(Order.ORDER_STATUS_NEW);
    }

    // 执行中订单
    public static long allZhixingOrderNums() {
        OrderService orderService = Config.getWac().getBean(OrderService.class);
        return orderService.countByStatus(Order.ORDER_STATUS_YIPAIDAN, Order.ORDER_STATUS_BUSY);
    }

    // 今日 已完成订单总数
    public static long todayAllOrderNums() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);

        Setting setting = settingUtils.get();

        // 查询今日订单
        // Calendar calendaer = Calendar.getInstance();
        // calendaer.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
        // calendaer.set(Calendar.MINUTE, 0);
        // calendaer.set(Calendar.MILLISECOND, 0);
        // Date end = calendaer.getTime();
        // Date begin = DateUtils.addHours(end, -24);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -setting.getWorkBegin());
        calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date begin = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date end = calendar.getTime();

        OrderService orderService = Config.getWac().getBean(OrderService.class);
        return orderService.countFinishedByFinishTime(begin, end);
    }

    // 今日订单总收入
    public static BigDecimal todayAllOrderMoney() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);

        Setting setting = settingUtils.get();

        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(new Date());
        int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        // 昨日
        calendar.set(Calendar.HOUR_OF_DAY, setting.getWorkBegin());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayStart = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date lastDayEnd = calendar.getTime();
        if (setting.getWorkBegin() > nowHour) {
            calendar.add(Calendar.SECOND, 1);
            calendar.add(Calendar.DAY_OF_MONTH, -2);
            lastDayStart = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.SECOND, -1);
            lastDayEnd = calendar.getTime();
        }

        // 今日
        calendar.setTime(lastDayEnd);
        calendar.add(Calendar.SECOND, 1);
        Date thisDayStart = calendar.getTime();
        calendar.setTime(thisDayStart);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        Date thisDayEnd = calendar.getTime();

        OrderService orderService = Config.getWac().getBean(OrderService.class);

        BigDecimal money = orderService.sumFinishedMoneyByFinishTime(thisDayStart, thisDayEnd);
        if (null == money) {
            return BigDecimal.ZERO;
        }
        return money;
    }

    // 空闲代驾员
    public static long kongxianDriverNums() {
        DriverService driverService = Config.getWac().getBean(DriverService.class);
        return driverService.countByStatus(Driver.DRIVER_ONLINE);
    }

    // 忙碌代驾员
    public static long mangluDriverNums() {
        DriverService driverService = Config.getWac().getBean(DriverService.class);
        return driverService.countByStatus(Driver.DRIVER_BUSY);
    }

    // 司机拒单
    public static long allWaitOrderNums() {
        OrderService orderService = Config.getWac().getBean(OrderService.class);
        return orderService.countByStatus(Order.ORDER_STATUS_REFUSED);
    }

    // 获取系统KEY
    public static String getSystemAppKey() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);
        Setting setting = settingUtils.get();
        return setting.getSecKey();
    }

    // 获取主公司简称
    public static String getMainCompanyJc() {
        Company company = null;
        try {
            CompanyService companyService = Config.getWac().getBean(CompanyService.class);
            company = companyService.getMain();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (null != company) {
            return company.getAbbreviation();
        }
        return "";
    }

    // 获取管理后台域名
    public static String getWebUrl() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);
        String webUrl = settingUtils.get().getWebUrl();
        if (StringUtils.isNotBlank(webUrl)) {
            webUrl = webUrl.replaceAll("http://", "");
        }
        return webUrl;
    }

    // 待处理司机数量
    public static long waitedOperatingDriverAmount() {
        DriverApplyService driverApplyService = Config.getWac().getBean(DriverApplyService.class);
        DriverService driverService = Config.getWac().getBean(DriverService.class);
        long applyAmount = driverApplyService.countAll();// 申请司机
        long lessAmount = driverService.countAllLess();// 余额少于200
        return applyAmount + lessAmount;
    }

    // 余额少于200司机数量
    public static long lessDriverAmount() {
        DriverService driverService = Config.getWac().getBean(DriverService.class);
        return driverService.countAllLess();
    }

    // 申请司机数量
    public static long applyDriverAmount() {
        DriverApplyService driverApplyService = Config.getWac().getBean(DriverApplyService.class);
        return driverApplyService.countAll();
    }

    // 申请客户数量
    public static long applyMemberAmount() {
        MemberApplyService memberApplyService = Config.getWac().getBean(MemberApplyService.class);
        return memberApplyService.countAll();
    }

    // 最低上班金额
    public static Double getLowestWorkVirtual() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);
        return settingUtils.get().getLowestWorkVirtual();
    }

    // 未开发票的数目
    public static long getReceiptNumber() {
        ReceiptService receiptService = Config.getWac().getBean(ReceiptService.class);
        return receiptService.findReceiptNumber();
    }

    // 拒单数量
    public static long refusedOrderAmount() {
        OrderService orderService = Config.getWac().getBean(OrderService.class);
        return orderService.countRefused();
    }

    // 是否开启自动派单
    public static boolean autoPaidan() {
        SettingUtils settingUtils = Config.getWac().getBean(SettingUtils.class);

        return settingUtils.get().isAutoPaidan();
    }

    // 安卓客户端地址
    public static String androidCustomer() {
        AppVersionService appVersionService = Config.getWac().getBean(AppVersionService.class);

        AppVersion androidcustomer = appVersionService
            .findByAppVersionType(AppVersion.AppVersionType.androidcustomer);

        return androidcustomer.getDownloadurl();
    }

    // ios客户端地址
    public static String iosCustomer() {
        AppVersionService appVersionService = Config.getWac().getBean(AppVersionService.class);

        AppVersion ioscustomer = appVersionService
            .findByAppVersionType(AppVersion.AppVersionType.ioscustomer);

        return ioscustomer.getDownloadurl();
    }

    public static String getLanguage(String keyWords, String language) {
        try {
            Properties prop = PropertiesLoaderUtils
                .loadAllProperties("language/" + language + ".properties");
            return prop.getProperty(keyWords);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    //获取layout城际专线显示权限
    public static boolean hasPermission_Zhuanxian() {
        Subject currentUser = SecurityUtils.getSubject();

        boolean show = false;
        if (currentUser.isPermitted("zhuanxianCar") || currentUser.isPermitted("zhuanxianLine")) {
            show = true;
        }

        return show;
    }

    //获取layout租车显示权限
    public static boolean hasPermission_Zuche() {
        Subject currentUser = SecurityUtils.getSubject();

        boolean show = false;
        if (currentUser.isPermitted("rentOrder") || currentUser.isPermitted("car") || currentUser
            .isPermitted("carType") || currentUser.isPermitted("brand")) {
            show = true;
        }

        return show;
    }

    //获取layout加油站显示权限
    public static boolean hasPermission_Gasstation() {
        Subject currentUser = SecurityUtils.getSubject();

        boolean show = false;
        if (currentUser.isPermitted("gasstation")) {
            show = true;
        }

        return show;
    }
}
